/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.util.Callback
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.BeanUtils
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.salesReceipts.GetGiftPdAndExpOutData;
import com.gys.data.salesReceipts.GetPdAndExpOutData;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.gys.data.salesReceipts.GiftData;
import com.gys.view.salesReceipts.GiftPdAndExpView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class GiftPdAndExpController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(GiftPdAndExpController.class);
    @FXML
    private AnchorPane pane;
    @FXML
    private TableView<GetGiftPdAndExpOutData> tableView;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> gssbVaildDate;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> proCode;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> gssbBatchNo;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> giveNum;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> gssbQty;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> proName;
    @FXML
    private TableColumn<GetGiftPdAndExpOutData, String> numReal;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public GiftData giftData;
    private boolean flag = false;
    private ObservableList<GetGiftPdAndExpOutData> observableTableList = FXCollections.observableArrayList(new ArrayList());

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.bangding();
        this.addTextField(this.numReal);
        this.init();
    }

    @Override
    public void init() {
        int i;
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("nameOrCode", this.giftData.getGiftCode());
        HttpJson httpJson = SalesReceiptsUtil.queryBatchNoAndExp(param);
        List outData = null;
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        outData = JSON.parseArray((String)httpJson.getMsg(), GetGiftPdAndExpOutData.class);
        ObservableList items = this.salesReceiptsMainController.getTableView().getItems();
        ArrayList<GetGiftPdAndExpOutData> remove = new ArrayList<GetGiftPdAndExpOutData>();
        for (i = 0; i < items.size(); ++i) {
            GetSalesReceiptsTableOutData item = (GetSalesReceiptsTableOutData)items.get(i);
            if (!this.giftData.getGiftCode().equals(item.getProCode()) || ObjectUtil.isEmpty(item.getPdAndExpData())) continue;
            for (int j = 0; j < outData.size(); ++j) {
                GetGiftPdAndExpOutData getPdAndExpOutData = (GetGiftPdAndExpOutData)outData.get(j);
                if (!getPdAndExpOutData.getGssbBatchNo().equals(item.getPdAndExpData().getGssbBatchNo())) continue;
                Float count = Float.valueOf(Float.valueOf(getPdAndExpOutData.getGssbQty()).floatValue() - Float.valueOf(item.getNum()).floatValue());
                getPdAndExpOutData.setGssbQty(CommonUtil.format2(count.floatValue()));
                if (!(count.floatValue() <= 0.0f)) continue;
                remove.add(getPdAndExpOutData);
            }
        }
        outData.removeAll(remove);
        for (i = 0; i < outData.size(); ++i) {
            GetGiftPdAndExpOutData getGiftPdAndExpOutData = (GetGiftPdAndExpOutData)outData.get(i);
            getGiftPdAndExpOutData.setGiveNumD(this.giftData.getGiveNumReal());
            getGiftPdAndExpOutData.setGiveNum(this.giftData.getGiveNumReal());
        }
        this.observableTableList = FXCollections.observableArrayList((Collection)outData);
        this.tableView.setItems(this.observableTableList);
        this.tableView.refresh();
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                GiftPdAndExpView view = SpringUtil.getBean(GiftPdAndExpView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            GiftPdAndExpController.this.sureAction();
                        } else if (e.getCode() == KeyCode.UP) {
                            int index;
                            ObservableList outData = GiftPdAndExpController.this.tableView.getSelectionModel().getSelectedItems();
                            if (outData.size() > 1 && (index = GiftPdAndExpController.this.tableView.getSelectionModel().getSelectedIndex()) >= 1) {
                                GiftPdAndExpController.this.tableView.getSelectionModel().select(index - 1);
                            }
                            GiftPdAndExpController.this.tableView.requestFocus();
                        } else if (e.getCode() == KeyCode.DOWN) {
                            int index;
                            ObservableList outData = GiftPdAndExpController.this.tableView.getSelectionModel().getSelectedItems();
                            if (outData.size() > 1 && (index = GiftPdAndExpController.this.tableView.getSelectionModel().getSelectedIndex()) < outData.size() - 1) {
                                GiftPdAndExpController.this.tableView.getSelectionModel().select(index + 1);
                            }
                            GiftPdAndExpController.this.tableView.requestFocus();
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
    }

    private void addTextField(TableColumn<GetGiftPdAndExpOutData, String> column) {
        column.setCellFactory(tc -> {
            final TextField textField = new TextField();
            TableCell<GetGiftPdAndExpOutData, String> cell = new TableCell<GetGiftPdAndExpOutData, String>(){

                protected void updateItem(String chuzhi, boolean empty) {
                    super.updateItem(chuzhi, empty);
                    if (empty) {
                        this.setGraphic(null);
                    } else {
                        if (ObjectUtil.isNotEmpty(chuzhi)) {
                            GiftPdAndExpController.this.flag = true;
                            textField.setText(chuzhi);
                            GiftPdAndExpController.this.flag = false;
                        }
                        this.setGraphic((Node)textField);
                    }
                }
            };
            textField.textProperty().addListener((ChangeListener)new ChangeListener<String>(){
                final TableCell val_cell;
                final TextField val_textField;
                {
                    this.val_cell = cell;
                    this.val_textField = textField;
                }

                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (GiftPdAndExpController.this.flag) {
                        return;
                    }
                    int index = this.val_cell.getIndex();
                    GetGiftPdAndExpOutData giftData = (GetGiftPdAndExpOutData)GiftPdAndExpController.this.observableTableList.get(index);
                    if (ObjectUtil.isNotEmpty(newValue) && !CommonUtil.positiveInteger(newValue)) {
                        this.val_textField.setText(oldValue);
                        return;
                    }
                    this.val_textField.setText(newValue);
                    giftData.setNumReal(newValue);
                    boolean re = GiftPdAndExpController.this.count(giftData);
                    if (!re) {
                        GiftPdAndExpController.this.flag = true;
                        this.val_textField.setText(oldValue);
                        GiftPdAndExpController.this.flag = false;
                        return;
                    }
                }
            });
            return cell;
        });
    }

    private boolean count(GetGiftPdAndExpOutData giftData) {
        int count = 0;
        for (int i = 0; i < this.observableTableList.size(); ++i) {
            GetGiftPdAndExpOutData giftDataTemp = (GetGiftPdAndExpOutData)this.observableTableList.get(i);
            String numReal = giftDataTemp.getNumReal();
            if (!ObjectUtil.isNotEmpty(numReal)) continue;
            count += Integer.valueOf(numReal).intValue();
        }
        String giveNumD = giftData.getGiveNumD();
        int num = Integer.valueOf(giveNumD) - count;
        if (num < 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u5151\u6362\u6570\u91cf\u8d85\u8fc7\u5f53\u524d\u53ef\u5151\u6362\u6700\u5927\u6570\u91cf");
            alert.showAndWait();
            return false;
        }
        for (int i = 0; i < this.observableTableList.size(); ++i) {
            GetGiftPdAndExpOutData getGiftPdAndExpOutData = (GetGiftPdAndExpOutData)this.observableTableList.get(i);
            getGiftPdAndExpOutData.setGiveNum(String.valueOf(num));
        }
        return true;
    }

    private void bangding() {
        this.proCode.setCellValueFactory((Callback)new PropertyValueFactory(this.proCode.getId()));
        this.proName.setCellValueFactory((Callback)new PropertyValueFactory(this.proName.getId()));
        this.gssbQty.setCellValueFactory((Callback)new PropertyValueFactory(this.gssbQty.getId()));
        this.gssbBatchNo.setCellValueFactory((Callback)new PropertyValueFactory(this.gssbBatchNo.getId()));
        this.gssbVaildDate.setCellValueFactory((Callback)new PropertyValueFactory(this.gssbVaildDate.getId()));
        this.giveNum.setCellValueFactory((Callback)new PropertyValueFactory(this.giveNum.getId()));
        this.numReal.setCellValueFactory((Callback)new PropertyValueFactory(this.numReal.getId()));
    }

    @FXML
    private void sureAction() {
        for (int i = 0; i < this.tableView.getItems().size(); ++i) {
            GetGiftPdAndExpOutData getGiftPdAndExpOutData = (GetGiftPdAndExpOutData)this.tableView.getItems().get(i);
            if (ObjectUtil.isEmpty(getGiftPdAndExpOutData.getNumReal()) || "0".equals(getGiftPdAndExpOutData.getNumReal())) continue;
            GetSalesReceiptsTableOutData detailOutData = this.salesReceiptsMainController.queryPro(this.giftData.getGiftCode());
            if (ObjectUtil.isEmpty(detailOutData)) {
                return;
            }
            detailOutData.setIsGift("1");
            this.salesReceiptsMainController.getProUsage().setText(detailOutData.getProUsage());
            this.salesReceiptsMainController.getProContraindication().setText(detailOutData.getProContraindication());
            detailOutData.setNum(getGiftPdAndExpOutData.getNumReal());
            String param4 = this.giftData.getParam4();
            if ("gift1".equals(param4)) {
                if (ObjectUtil.isNotEmpty(this.giftData.getAddPrice())) {
                    String addPrice = this.giftData.getAddPrice();
                    detailOutData.setPrice(addPrice);
                    float v1 = Float.valueOf(addPrice).floatValue() / Float.valueOf(detailOutData.getPrcAmount()).floatValue() * 100.0f;
                    detailOutData.setDisc((int)v1 + "");
                    float v = Float.valueOf(addPrice).floatValue() * (float)Integer.valueOf(detailOutData.getNum()).intValue();
                    detailOutData.setTotal(CommonUtil.format2(v));
                    detailOutData.setStatus("\u4fc3\u9500");
                } else if (ObjectUtil.isNotEmpty(this.giftData.getAddDisc())) {
                    String addDisc = this.giftData.getAddDisc();
                    Float s = Float.valueOf(Float.valueOf(detailOutData.getPrcAmount()).floatValue() * Float.valueOf(addDisc).floatValue() / 100.0f);
                    detailOutData.setPrice(CommonUtil.format2(s.floatValue()));
                    detailOutData.setDisc(addDisc);
                    float v = Float.valueOf(detailOutData.getPrice()).floatValue() * (float)Integer.valueOf(detailOutData.getNum()).intValue();
                    detailOutData.setTotal(CommonUtil.format2(v));
                    detailOutData.setStatus("\u4fc3\u9500");
                }
            } else if ("gift0".equals(param4)) {
                detailOutData.setPrice("0.01");
                detailOutData.setDisc("0");
                detailOutData.setTotal(0.01 * (double)Integer.valueOf(detailOutData.getNum()).intValue() + "");
                detailOutData.setStatus("\u4fc3\u9500");
            }
            GetPdAndExpOutData pdAndExpData = new GetPdAndExpOutData();
            BeanUtils.copyProperties(getGiftPdAndExpOutData, pdAndExpData);
            detailOutData.setPdAndExpData(pdAndExpData);
            ObservableList items = this.salesReceiptsMainController.getTableView().getItems();
            this.salesReceiptsMainController.covertMoneyFormat(detailOutData);
            items.add(detailOutData);
            for (int j = 0; j < items.size(); ++j) {
                ((GetSalesReceiptsTableOutData)items.get(j)).setIndex(String.valueOf(j + 1));
            }
            this.salesReceiptsMainController.getTableView().refresh();
            this.salesReceiptsMainController.countMoney();
        }
        this.cancelAction();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

